package com.tzxs.pay.util.netnavigator;

import com.tzxs.pay.util.SingleLiveEvent;
import com.tzxs.pay.util.viewmodel.Callback;

public abstract class SimpleNetNavigator<T> extends NetNavigator<T> {
    //success方法我们最好回调回去,所以不用实现
    //利用LiveData来显示信息
    private SingleLiveEvent<String> snackbarEvent;
    private SingleLiveEvent<String> dialogEvent;
    private Callback callback;

    protected SimpleNetNavigator(SingleLiveEvent<String> snackbarEvent, SingleLiveEvent<String> dialogEvent, Callback callback) {
        this.snackbarEvent = snackbarEvent;
        this.dialogEvent = dialogEvent;
        this.callback=callback;
    }

    @Override
    public void success(T data) {
        if (callback != null) {
            callback.success();
        }
    }

    @Override
    public void fail(final String error) {
        //加上空判断
        if (snackbarEvent != null)
            snackbarEvent.setValue(error);
    }

    @Override
    public void start(String title) {
        //我这个类型的开始了,就是显示一个dialog
        if (dialogEvent != null)
            dialogEvent.setValue(title);
    }

    @Override
    public void end() {
        if (dialogEvent != null)
            dialogEvent.call();
    }
}
